﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.MaHaiBo.Entity.Traceability;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Service.TraceabilityService;
using RBAC.MaHaiBo.API.DTOS;
using MediatR;
using RBAC.MaHaiBo.API.Application.Command.SlaughterCommand;

namespace RBAC.MaHaiBo.API.Controllers
{
    /// <summary>
    /// 屠宰控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SlaughterController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<SlaughterController> logger;
        /// <summary>
        /// 屠宰服务层
        /// </summary>
        ISlaughterService service;
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="service">屠宰服务层</param>
        /// <param name="mediator">中介者</param>
        public SlaughterController(ILogger<SlaughterController> logger,
        ISlaughterService service,
        IMediator mediator)
        {
            this.logger = logger;
            this.service = service;
            this.mediator = mediator;
        }


        /// <summary>
        /// 添加屠宰信息
        /// </summary>
        /// <param name="slaughter">要添加的数据信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddSlaughter(AddSlaughterCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code=await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message= ex.Message;
                logger.LogError("【屠宰服务层】 添加屠宰信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改屠宰信息
        /// </summary>
        /// <param name="slaughterId">修改后的屠宰信息</param>
        /// <returns>返回受影响行数 </returns>
        [HttpPut]
        public async Task<ApiResult<int>> UPdSlaughter(UPdSlaughterCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【屠宰服务层】 修改屠宰信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示屠宰信息
        /// </summary>
        /// <param name="LOT"></param>
        /// <returns>返回数据信息</returns>
        [HttpGet]
        public async Task<ApiResult<List<Slaughter>>> GetSlaughter([FromQuery] GetSlaughterCommand request)
        {
            ApiResult<List<Slaughter>> result = new ApiResult<List<Slaughter>>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【屠宰服务层】 修改屠宰信息 出错" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 添加屠宰企业信息
        /// </summary>
        /// <param name="firm">企业信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> AddSlaughterFirm(AddSlaughterFirmCommand request)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【屠宰服务层】 添加屠宰企业信息 出错" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示屠宰企业信息
        /// </summary>
        /// <returns>返回数据集合</returns>
        [HttpGet]
        public async Task<ApiResult<List<SlaughterFirmDTO>>> GetSlaughterFirm([FromQuery]GetSlaughterFirmCommand request)
        {
            ApiResult<List<SlaughterFirmDTO>> result = new ApiResult<List<SlaughterFirmDTO>>();
            try
            {
                result.Data = await  mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【屠宰服务层】 显示屠宰企业信息 出错" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
